package com.jtech.dribbble.net.api;

import com.google.gson.FieldNamingPolicy;
import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
import com.google.gson.internal.bind.DateTypeAdapter;
import com.jtech.dribbble.common.Constants;
import com.jtech.dribbble.net.call.JCallHander;

import java.util.Date;

import okhttp3.OkHttpClient;
import retrofit2.Retrofit;
import retrofit2.converter.gson.GsonConverterFactory;

/**
 * 作为接口的实现类
 * Created by wuxubaiyang on 16/4/17.
 */
public class API {
    /**
     * 持有演示用api对象
     */
    private static TestApi testApi;

    /**
     * 获取演示用api对象
     *
     * @return
     */
    public static TestApi TestApi() {
        if (null == testApi) {
            //创建gson
            Gson gson = new GsonBuilder()
                    .setFieldNamingPolicy(FieldNamingPolicy.LOWER_CASE_WITH_UNDERSCORES)
                    .registerTypeAdapter(Date.class, new DateTypeAdapter())
                    .create();
            //创建okhttp
            OkHttpClient okHttpClient = new OkHttpClient();
            //创建retrofit
            Retrofit retrofit = new Retrofit.Builder()
                    .addCallAdapterFactory(new JCallHander.JCallAdapterFactory())
                    .addConverterFactory(GsonConverterFactory.create(gson))
                    .baseUrl(Constants.BASE_URL)
                    .client(okHttpClient)
                    .build();
            //创建retrofit
            testApi = retrofit.create(TestApi.class);
        }
        return testApi;
    }
}